<?php
/**
 * DESC: 商品模块
 * 作者:晨光
 * EMAIL:461718846@qq.com
 * DATE:2020/5/16
 */

namespace app\admin\controller;


use app\common\controller\AdminUserBase;
use app\common\model\GoodsCate;
use think\Db;
use think\facade\Request;

class Goods extends AdminUserBase
{
    /**
     * 商品分类
     */
    public function goods_cate(){
        if (Request::isAjax()){
            $data = GoodsCate::order('sort asc')->append(['open'])->select();
            $this->result($data);
        }
        return view('cate');
    }
    public function goods_cate_tree(){
        $data = GoodsCate::order('sort asc')->select()->toArray();
        $this->result(array2tree($data));
    }
    public function goods_cate_save(){
        $param = input();

        $model = new GoodsCate();
        if (empty($param['id'])){
            $res = $model->allowField(true)->isUpdate(false)->save($param);
            saveAdminLog("添加商品分类");
        }else{
            $res = $model->allowField(true)->isUpdate(true)->save($param);
            saveAdminLog("编辑商品分类");
        }

        if ($res !== false){
            $this->success();
        }else{
            $this->error();
        }
    }
    public function goods_cate_del(){
        $id = input('id');
//        判断当前节点下有没有子节点
        $ziNum = Db::name('GoodsCate')->where('pid',$id)->count();
        if ($ziNum > 0){
            $this->error('当前节点下有子分类无法删除，请先删除子分类');
        }
        $res = Db::name('GoodsCate')->delete($id);
        if ($res !== false){
            saveAdminLog("删除商品分类");
            $this->success();
        }else{
            $this->error();
        }
    }
    /**
     * 商品分类
     */

    /**
     * 商品列表
     */
    public function goods(){
        if (Request::isAjax()){
            $param = input();
            $where = [];
            $hasWhere = [];
            if (!empty($param['title'])){
                $where[] = ['title','like','%'.$param['title'].'%'];
            }
            if (!empty($param['article_cate_id'])){
                $hasWhere[] = ['article_cate_id','in',$param['article_cate_id']];
            }
            if (!empty($param['top'])){
                $where[] = ['top','=',$param['top']];
            }
            if (!empty($param['show'])){
                $where[] = ['show','=',$param['show']];
            }
            $data = \app\common\model\Goods::hasWhere('cateRelated',$hasWhere)->where($where)->order('sort desc')->paginate($param['limit']);
            $data = $data->toArray();
            $this->result($data['data'],$data['total']);
        }
        return view('goods/goods/index');
    }
    public function form(){
        $id = input('id');
        $data = \app\common\model\Goods::get($id);
        if (!empty($data)){
            $data['cate_id'] = $data->cateRelated()->column('goods_cate_id');
            $data['cate_id'] = json_encode($data['cate_id']);
        }
        $this->assign('data',$data);

        return view('goods/goods/form');
    }
    /**
     * 商品列表
     */
}
